import BaseView from "../../../cfw/mvc/BaseView";
import BaseAd from "../../../sdk/base/BaseAd";
import SDKManager from "../../../sdk/SDKManager";
import BaseNativeAd from "../../../sdk/base/BaseNativeAd";
import BaseNativeAdItemModel from "../../../sdk/base/BaseNativeAdItemModel";
import SNativeAdView from "./SNativeAdView";
import UIManager from "../../../cfw/ui/UIManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class OVNativeAdView extends SNativeAdView {


	@property({ type: cc.Button, displayName: "click$VButton" })
	click$VButton: cc.Button = null;

	@property({ type: cc.Label, displayName: "text$VLabel" })
	text$VLabel: cc.Label = null;





	start() {
		let nativeAd: BaseNativeAd = SDKManager.getChannel().getNativeAd()
		if (nativeAd) {
			if (SDKManager.getChannel().isOpenNativeErrorClick()) {
				this.text$VLabel.string = '点击查看'
			} else {
				this.text$VLabel.string = '点击跳过'
			}

			SDKManager.getChannel().showNativeAd(0, (modelList: BaseNativeAdItemModel[]) => {
				if (modelList.length > 0) {
					this.model = modelList[0]
					SDKManager.getChannel().reportAdShow(this.model.getID())
					let icon = this.model.getRandomImage()
					if (icon) {
						cc.assetManager.loadRemote(icon, (err, res: cc.Texture2D) => {
							if (err || !cc.isValid(this.node)) {
								return;
							}
							this.icon.spriteFrame = new cc.SpriteFrame(res)
						})
					}
				} else {
					this.node.active = false;
				}

			})
		}
	}




	onDestroy() {

	}

	onback$VButtonClick() {
		this.hide()
		SDKManager.getChannel().loadNativeAd()
	}

	onclick$VButtonClick() {
		let nativeAd: BaseNativeAd = SDKManager.getChannel().getNativeAd()
		if (nativeAd) {
			if (SDKManager.getChannel().isOpenNativeErrorClick()) {
				this.onback$VButtonClick()
				this.onNativeAdClick()
			} else {
				this.onback$VButtonClick()
			}
		} else {
			this.onback$VButtonClick()
		}
	}

	// onNativeAdClick() {
	// 	if (this.model) {
	// 		SDKManager.getChannel().reportAdClick(this.model.getID())

	// 	}
	// }

    onNativeAdClick() {
        if (this.model) {
			this.hide()
            SDKManager.getChannel().reportAdClick(this.model.getID())

        }
    }

}